package leaf.science.pages
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import leaf.science.Global;
	import leaf.science.nav.Page;
	import leaf.science.ui.camera.DemoHotspot;
	import leaf.science.ui.camera.DemoToolTip;
	import leaf.science.ui.camera.DemosCamera;
	import leaf.science.ui.demoplayer.DemosPlayer;

	public class PageDemos extends JogPageBase
	{
		public var demosPlayer:DemosPlayer;
		
		public function PageDemos()
		{
			super();
		}
		
		override protected function cameraReady():void
		{
			super.cameraReady();
			rightMenu.open();
			camera.addEventListener( MouseEvent.CLICK, onCameraClick );
			var page:Page = LeafScience.instance.currentPage;
			if (LeafScience.instance.currentPage == this) camera.showHotspots();
		}
		
		override protected function closeAllOverlays():void
		{
			if (demosPlayer.isOpen) demosPlayer.close();
			camera.closeAllToolTipsNow();
			setTimeout(hideHotspots, 100);
		}
		
		private function hideHotspots():void
		{
			if( camera.hotspotsVisible ) camera.hideHotspots();
			setTimeout(onOverlayClosed, 600);
		}
		
		override protected function onRightSelect (evt:Event):void
		{
			DemosCamera(camera).closeAllToolTipsNow();
			removeJogTip();
			DemosCamera(camera).hotspotId = rightMenu.selectedID;
		}
		
		override protected function onRightChange (evt:Event):void
		{
			DemosCamera(camera).closeAllToolTipsNow();
			DemosCamera(camera).hotspotId = rightMenu.selectedID;
		}
		
		
		override protected function onKeyUp(evt:KeyboardEvent):void
		{
			if (!demosPlayer.isOpen) rightMenu.onKeyUp(evt);
		}
		
		override protected function onCameraClick (evt:MouseEvent):void
		{
			if (!evt.target is DemoHotspot && !evt.target is DemoToolTip) return;
			
			
			if( evt.target is DemoHotspot )
			{
				rightMenu.gotoID ( DemoHotspot(evt.target).id );
			}
			if( evt.target is DemoToolTip )
			{
				rightMenu.gotoID ( DemoToolTip( evt.target ).hotspot.id );
			}
			
			camera.closeAllToolTipsNow();
			removeJogTip();
			
			var data:XML = Global.DATA_DEMOS..demo.(id==rightMenu.selectedID)[0];
			demosPlayer.title = data.title;
			demosPlayer.source = data.movie;
			setTimeout(demosPlayer.open, 400);
		}
		
		
		override protected function init ():void
		{
			_message = "Choose a Demo";
			_data = Global.DATA_DEMOS;
						
			_menuItems = [];
			for each (var item:XML in _data..demo){
				_menuItems.push({id:uint(item.id), title:item.title.toString()});
			}
			
			super.init();
			
			demosPlayer = new DemosPlayer();
			addChild(demosPlayer);
			
		}
		
		override public function toString():String
		{
			return 'demos';
		}
		
		
	}
}